/*
 * FormationGenerator.java
 *
 * Created on 21 de junio de 2007, 12:14 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Behaviors.Formations;
import Utils.Vector2d;
/**
 *
 * @author jonathan
 */
public class FormationGenerator {
    public static final int SQUARE=0;
    public static final int COLUMN=1;
    public static final int ROW=2;
    public static final int ARROW=3;
    private String currentFormation="SQUARE";
    private double currentWidth=100.0f;
    
    //public static final Vector2d [] posSquare={new Vector2d(1,1,0),new Vector2d(1,-1,0),new Vector2d(-1,-1,0),new Vector2d(-1,1,0)};
    //public static final Vector2d [] posColumn={new Vector2d(1.5f,0,0),new Vector2d(0.5f,0.0f,0),new Vector2d(-0.5f,0,0),new Vector2d(-1.5f,0,0)};
    //public static final Vector2d [] posRow={new Vector2d(0,0.5f,0),new Vector2d(0,-0.5f,0),new Vector2d(0,-1.5f,0),new Vector2d(0,1.5f,0)};
    //public static final Vector2d [] posArrow={new Vector2d(1.0f,0.5f,0),new Vector2d(1,-0.5f,0),new Vector2d(-1,-1,0),new Vector2d(-1,1,0)};
    
    private boolean init=false;
    private double width=100.0f;
    private Vector2d [] actualFormationVectors;
   
    private Formation actualFormation;
    
    private Formation[] formations;
     
    /**
     * Creates a new instance of FormationGenerator
     */
    public FormationGenerator(Formation[] formations) {
        
        this.formations = formations;
        actualFormationVectors=new Vector2d[formations[0].getNumRobots()];
        updatePositions(formations[0]);
                  
    }
    
    public void updatePositions(Formation formation){
        
        actualFormationVectors = new Vector2d[formation.getNumRobots()];
        formation.setWidth(width);
        
        actualFormation = formation;
        double scaleF = formation.getScaleFactor();
        
        //System.out.println("factor de escala: "+ scaleF);
        for(int i=0;i<formation.getNumRobots();i++)
        {
            actualFormationVectors[i] = formation.getRobotPosition(i).scale(scaleF);
           // System.out.println("Vector no. "+ i +": "+ actualFormationVectors[i].toString());
        }
        

    }
    
    /*
     * Este metodo debe usarse solo para cambios temporales luego de los cuales debe
     * llamarse al metodo returnToCurrentFormation() para volver a la formacion preestablecida
     * 
     */
     public boolean generatePositions(String formationName, double width, int numRobots){
          this.width=width;
          
          for(int i=0; i < formations.length; i++){
              if(formations[i].getName().equals(formationName)){
                  
                  formations[i].setNumRobots(numRobots);
                  updatePositions(formations[i]);
                  return true;
              }
          
          }
          
          return false;
          
     }
     
     public boolean setCurrentFormation(String formationName, float width, int numRobots){
        this.currentFormation=formationName;
        this.currentWidth=width;
        generatePositions(this.currentFormation, this.currentWidth, numRobots);
        return true;
     }
     
     public boolean returnToCurrentFormation(){
        generatePositions(this.currentFormation,this.currentWidth,4);
        return true;
     }
    
     public void setWidth(float width){
      
         this.width = width;
         updatePositions(formations[0]);
     }
     
     public double getWidth(){
      
         return width;    
     }
     
     public Vector2d getPositionForRobot(int id){
         if(id < actualFormationVectors.length)
            return actualFormationVectors[id];
         else
             return null;
     }
     
     public Formation getActualFormation(){
         return this.actualFormation;
     
     }
    
     
    
    
}